package com.heima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.dao.CheckItemDao;
import com.heima.entity.PageResult;
import com.heima.entity.QueryPageBean;
import com.heima.exception.CheckItemDeleteFailException;
import com.heima.pojo.CheckItem;
import com.heima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {
    @Autowired
    private CheckItemDao checkItemDao;

    @Override
    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<CheckItem> checkItems = checkItemDao.selectByCondition(queryString);

        long total = checkItems.getTotal();
        List<CheckItem> rows = checkItems.getResult();

        return new PageResult(total,rows);
    }

    @Override
    public void deleteById(Integer id) throws CheckItemDeleteFailException {
        long count = checkItemDao.findCountByCheckItemId(id);
        if (count > 0){
            throw new CheckItemDeleteFailException();
        }
        checkItemDao.deleteById(id);
    }

    @Override
    public void edit(CheckItem checkItem) {
        checkItemDao.edit(checkItem);
    }

    @Override
    public List<CheckItem> findAll() {
        List<CheckItem> all = checkItemDao.findAll();
        return all;
    }

    @Override
    public List<CheckItem> findAllAndOrder(Integer id) {
        List<CheckItem> all = checkItemDao.findAllAndOrder(id);
        return all;
    }
}
